package cn.tedu.com.intercepter;

import cn.tedu.com.bean.ResponseEntity;
import cn.tedu.com.bean.UserInfo;
import cn.tedu.com.enums.ReturnCode;
import cn.tedu.com.enums.UserBaseRelation;
import cn.tedu.com.enums.UserRole;
import cn.tedu.com.util.AppUtil;
import cn.tedu.com.util.RedisPoolListenerUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;


/**
 * <p>
 *
 * </p>
 *
 * @author zhaoyue1
 * @Description
 * @since 2021-05-14
 */
@Component
public class AuthIntercepter implements HandlerInterceptor {

//    @Autowired
//    @Qualifier("srtZero")
//    private StringRedisTemplate stringRedisTemplate;

    public static void  main(String[] args) throws Exception {
        UserInfo userInfo=new UserInfo("name","131", UserRole.USER_LEADER, UserBaseRelation.USER_DB1,"name","usr","123");
        System.out.println(userInfo);
        String str= JSONObject.toJSONString(userInfo);
        System.out.println(str);
        UserInfo userInfo1 = JSONObject.parseObject(str, UserInfo.class);
        System.out.println(userInfo1);
        System.out.println("--"+userInfo1.getUserBaseRelation().getCode());
        AppUtil.addUserInfo(userInfo);
        UserInfo userInfo2 = AppUtil.getUserInfo();
        System.out.println(userInfo2);
        ResponseEntity responseEntity = new ResponseEntity(ReturnCode.BUSINESS_EXCEPTION);
        String s = JSONObject.toJSONString(responseEntity);
        System.out.println("hihihihi"+s);
//        System.out.println("++"+userInfo1.getReturnCodeEnum().getRetcode());
//        System.out.println(ReturnCodeEnum.ERROR_SYSTEM.getRetcode());
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Cookie[] cookies = request.getCookies();
        String token=null;
        if(Objects.isNull(cookies)||cookies.length<1){
            throw new Exception("获取 cookies 失败 无法验证");
        }
        for(Cookie cookie:cookies){
            String name = cookie.getName();
            if(AppUtil.TOKEN.equals(name)){
                token=cookie.getValue();
            }
        }
        if(StringUtils.isBlank(token)){
            throw new Exception("获取 cookie 值失败 无法验证");
        }
        String s = RedisPoolListenerUtil.getSRTDbByKey(RedisPoolListenerUtil.REDIS_STR_DB0).opsForValue().get(token);
        UserInfo userInfo = JSONObject.parseObject(s,UserInfo.class);
        if(Objects.isNull(userInfo)){
            throw new Exception("用户信息获取异常");
        }
        AppUtil.addUserInfo(userInfo);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
